import time

from selenium.webdriver.common.by import By

# 首页浏览商品
from common.base import Base


class HomePage(Base):
    url = 'http://139.129.26.163/index.php'

    #   立即购买按钮
    def buy_button(self):
        return self.base_find_element((By.CSS_SELECTOR, 'img[src="themes/default/images/buybtn1.png"]'))

    #   点击仓品
    def click_home_product(self):
        self.base_click((By.XPATH, "//a[contains(text(),'仓品')]"))
        time.sleep(0.5)

    #   输入购买数量，点击立即购买u
    def click_buy_button(self, n):
        self.base_select_all((By.CSS_SELECTOR, 'input[id="number"]'))
        self.base_send_keys((By.CSS_SELECTOR, 'input[id="number"]'), f'{n}')
        time.sleep(0.5)
        self.buy_button().click()

    #   点击收藏
    def collect(self):
        self.base_click((By.CSS_SELECTOR, 'img[src="themes/default/images/bnt_colles.gif"]'))
        time.sleep(1)
        self.driver.switch_to.alert.accept()

    #   分享
    def share(self):
        self.base_click((By.CSS_SELECTOR, 'img[src="themes/default/images/bnt_recommend.gif"]'))
        time.sleep(0.5)

    #   商品描述
    def describe(self):
        self.base_click((By.CSS_SELECTOR, 'div[id="com_b"] h2'))
        time.sleep(0.5)

    #   商品属性
    def attr(self):
        self.base_click((By.CSS_SELECTOR, 'div[id="com_b"] h2[class="h2bg"]'))
        time.sleep(1)

    #   点击商品标签
    def click_good_sign(self):
        self.base_click((By.CSS_SELECTOR, 'p[id="ECS_TAGS"] a'))
        time.sleep(0.5)

    #   点击购买过的商品
    def bud_goods(self):
        return self.base_find_elements((By.CSS_SELECTOR, 'div[class="goodsItem"]'))

    #   购买记录
    def bud_page_operation(self, where):
        if where == 'n':
            #   下一页
            self.base_click((By.LINK_TEXT, '下一页'))
        elif where == 'u':
            #   上一页
            self.base_click((By.LINK_TEXT, '上一页'))
        elif where == 'e':
            #   末页
            self.base_click((By.LINK_TEXT, '最末页'))
        elif where == 'f':
            #   第一页
            self.base_click((By.LINK_TEXT, '第一页'))
        else:
            print('没这操作')
